package com.fenghongzhang.day011;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity {
    private EditText edit;
    private Button btn;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            //给输入框赋值
            String s = (String) msg.obj;
            edit.setText(s);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        /**
         * 原理,
         *  * 1,写一个观察者,
         *  * 2,观察者一直在监视的短信的动态
         *  * 3,一旦发现有短信的变化(也就是短信的数据库里的数据有变化)
         *  * 4,就执行相应的业务逻辑
         */
        edit = (EditText) findViewById(R.id.edit);

        ContentResolver contentResolver = getContentResolver();
        /**
         * 1,uri
         */
        MyObverse myObverse = new MyObverse(handler,this);
        contentResolver.registerContentObserver(Telephony.Sms.CONTENT_URI,true,myObverse);

    }
}
